package cn.bytepulse.web.admin.controller;

import cn.bytepulse.annotation.Log;
import cn.bytepulse.pojo.AjaxResult;
import cn.bytepulse.web.admin.entity.SysUser;
import cn.bytepulse.web.admin.service.SystemUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author jiejiebiezheyang
 * @since 2024-07-10 22:00
 */
@Tag(name = "管理-系统管理-用户管理")
@RestController
@RequestMapping("/system/user")
@RequiredArgsConstructor
public class SystemUserController {

    private final SystemUserService systemUserService;

    @GetMapping("/list")
    @Operation(summary = "用户列表")
    @Log(value = "管理-系统管理-用户管理-用户列表", persist = true)
    @PreAuthorize("hasAnyAuthority('system:user:query')")
    public AjaxResult list(@RequestParam(required = false) Integer id) {
        return systemUserService.list(id);
    }

    @PostMapping("/add")
    @Operation(summary = "添加用户")
    @Log(value = "管理-系统管理-用户管理-添加用户", persist = true)
    @PreAuthorize("hasAnyAuthority('system:user:add')")
    public AjaxResult add(@RequestBody @Validated({SysUser.CreateUser.class}) SysUser sysUser) {
        return systemUserService.add(sysUser);
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户")
    @Log(value = "管理-系统管理-用户管理-更新用户", persist = true)
    @PreAuthorize("hasAnyAuthority('system:user:update')")
    public AjaxResult update(@RequestBody @Validated({SysUser.UpdateUser.class}) SysUser sysUser) {
        return systemUserService.update(sysUser);
    }

    @GetMapping("/getRelation")
    @Operation(summary = "获取关联")
    @Log(value = "管理-系统管理-用户管理-获取关联", persist = true)
    @PreAuthorize("hasAnyAuthority('system:user:relation:query')")
    public AjaxResult getRelation(@RequestParam Integer id) {
        return systemUserService.getRelation(id);
    }

    @GetMapping("/getRoleConcise")
    @Operation(summary = "获取角色简要列表")
    @Log(value = "管理-系统管理-用户管理-获取角色简要列表", persist = true)
    @PreAuthorize("hasAnyAuthority('system:user:relation:query')")
    public AjaxResult roleConcise() {
        return systemUserService.roleConcise();
    }

    @PostMapping("/updateRelation")
    @Operation(summary = "更新角色关联")
    @Log(value = "管理-系统管理-用户管理-更新角色关联", persist = true)
    @PreAuthorize("hasAnyAuthority('system:user:relation:update')")
    public AjaxResult updateRelation(@RequestParam Integer userId, Integer[] roleIds) {
        return systemUserService.updateRelation(userId, roleIds);
    }

    @PutMapping("/resetPassword")
    @Operation(summary = "重置用户密码")
    @Log(value = "管理-系统管理-用户管理-重置用户密码", persist = true)
    @PreAuthorize("hasAnyAuthority('system:user:password:reset')")
    public AjaxResult resetPassword(@RequestParam Integer id) {
        return systemUserService.resetPassword(id);
    }
}
